<template>
  <div>
    <!-- 查询 -->
    <el-form
      :model="search"
      status-icon
      ref="searchForm"
      class="demo-form-inline"
      style="margin-top: 20px"
      :inline="true"
    >
      <el-form-item prop="name">
        <el-input
          v-model="search.name"
          placeholder="根据姓名查询"
          style="width: 150px"
        ></el-input>
      </el-form-item>
      <el-form-item prop="adminName">
        <el-select
          v-model="search.adminName"
          placeholder="根据管理员查询"
          style="width: 150px"
        >
          <el-option
            v-for="option in search.plotOptions"
            :key="option._id"
            :label="option.name"
            :value="option.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="searchData('searchForm')"
          icon="el-icon-refresh"
          >查询</el-button
        >
        <el-button
          type="primary"
          @click="resetForm('searchForm')"
          icon="el-icon-warning"
          >重置</el-button
        >
        <router-link to="/rest/update/-1" tag="el-button">添加住户</router-link>
      </el-form-item>
    </el-form>
    <!-- 创建用户 -->
    <el-table :data="restList" style="width: 100%">
      <el-table-column type="index" label="序号" width="50" align="center">
      </el-table-column>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="住户姓名">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="性别">
              <span>{{ props.row.gender | dataFilter(genderOptions) }}</span>
            </el-form-item>
            <el-form-item label="年龄">
              <span>{{ props.row.age }}</span>
            </el-form-item>
            <el-form-item label="小区位置">
              <span>{{ props.row.plotName }}</span>
            </el-form-item>
            <el-form-item label="身份证号">
              <span>{{ props.row.id_number }}</span>
            </el-form-item>
            <el-form-item label="电话号">
              <span>{{ props.row.phone }}</span>
            </el-form-item>
            <el-form-item label="微信">
              <span>{{ props.row.vx }}</span>
            </el-form-item>
            <el-form-item label="详细住址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="照片列表">
              <el-image
                v-for="(item, index) in props.row.pictures"
                :key="index"
                :src="baseUrl + '/upload/' + item"
                :preview-src-list="setSrcList(props.row.pictures)"
                style="width: 100px; height: 100px；
                margin-right:10px;
                padding:5px;
                border:1px solid #ccc
                "
              >
              </el-image>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ props.row.note_String }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="住户姓名" prop="name" align="center">
      </el-table-column>
      <el-table-column label="性别" prop="gender" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.gender | dataFilter(genderOptions) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电话号" prop="phone" align="center">
      </el-table-column>
      <el-table-column label="管理员" prop="adminRest" align="center">
      </el-table-column>
      <el-table-column label="所在区域" prop="plotName" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row._id)"
            >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPage"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import restApi from "../../api/rest";
import areaApi from "@/api/area";
export default {
  data() {
    return {
      search: {
        name: "",
        adminRest: "",
        adminName: "",
        phone: "",
        plotOptions: [],
      },
      baseUrl: process.env.VUE_APP_SERVICE_URL,
      total: 0,
      size: 5, //当前页个数
      currentPage: 1, //当前页
      restList: [], //存储住户列表信息
      // 性别列表
      genderOptions: [
        { type: "1", name: "男" },
        { type: "0", name: "女" },
      ],
    };
  },
  // 过滤器
  filters: {
    dataFilter(type, dataOptions) {
      // console.log(type);
      const obj = dataOptions.find((obj) => obj.type == type);
      return obj ? obj.name : null;
    },
  },
  mounted() {
    this.gainRestList();
    this.getplotOptions();
    // console.log(this.baseUrl);
  },
  components: {},
  methods: {
    // 编辑窗口
    handleEdit(_id) {
      this.$router.push(`/rest/update/${_id}`);
    },
    // 获取区域列表
    getplotOptions() {
      areaApi.getareasList().then((response) => {
        const res = response.data;
        // console.log(res);
        if (res.status == 0) {
          this.search.plotOptions = res.data;
          //   console.log(this.search.plotOptions);
        }
      });
    },
    //重置按钮
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //条件查询
    searchData(formName) {
      console.log(this.search);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          restApi
            .getRestList(this.currentPage, this.size, this.search)
            .then((response) => {
              const res = response.data;
              if (res.status == 0) {
                this.restList = res.data.data;
                if (this.restList == 0) {
                  this.$message({
                    message: "用户不存在",
                    type: "warning",
                  });
                }
              } else {
                this.$message({
                  message: "获取异常",
                  type: "warning",
                });
              }
            });
        }
      });
    },
    // 处理列表中的图片
    setSrcList(imgList) {
      // console.log(imgList);
      return imgList.map((item) => this.baseUrl + "/upload/" + item);
    },
    openDialog() {},
    //查询住户信息
    gainRestList() {
      restApi
        .getRestList(this.currentPage, this.size, this.search)
        .then((result) => {
          const res = result.data;
          // console.log(res);
          if (res.status == 0) {
            this.restList = res.data.data;
            this.total = res.data.total;
            if (this.restList == 0) {
              this.currentPage = this.currentPage - 1;
              this.gainRestList();
            }
          } else {
            this.$message({
              message: res.msg,
              type: "warning",
            });
          }
        });
    },
    handleSizeChange(size) {
      this.size = size;
      this.gainRestList();
    },
    handleCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.gainRestList();
    },
    // 删除住户
    handleDelete(index, row) {
      restApi.deleteRestlist(row._id).then((result) => {
        const res = result.data;
        if (res.status == 0) {
          this.$message({
            message: res.data,
          });
          this.gainRestList();
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
  },
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-left: 90px;
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>